package xyz.molzhao.performance;

import xyz.molzhao.util.CommonUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

public class CustomThreadPool {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();

        CommonUtil.createBehavior("小明和小伙伴们 进餐厅点菜");
        long startTime = System.currentTimeMillis();

        CompletableFuture[] dishes = IntStream.rangeClosed(0, 99)
                .mapToObj(i -> new Dish("菜" + i, 1))
                .map(dish -> CompletableFuture.runAsync(dish::make, executorService))
                .toArray(CompletableFuture[]::new);

        // 等待所有任务执行完毕
        CompletableFuture.allOf(dishes).join();
        CommonUtil.createBehavior("菜都做好了，上桌" + (System.currentTimeMillis() - startTime));
    }
}
